package com.cuixichang.minimalism.core.mybatis.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseConfigParam;
import com.sun.istack.internal.NotNull;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CustomVelocityTemplateEngine extends VelocityTemplateEngine {
    @Override
    protected void outputCustomFile(@NotNull List<CustomFile> customFiles, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
        String uncapitalize = StringUtils.uncapitalize(tableInfo.getEntityName());
        objectMap.put("alias",uncapitalize);
        List<String> columnTypes = tableInfo.getFields().stream().map(tableField -> tableField.getColumnType().getType())
                .distinct().collect(Collectors.toList());
        objectMap.put("columnTypes", columnTypes);
        if(CollectionUtil.isNotEmpty(customFiles)){
            String root = (String) objectMap.getOrDefault(BaseConfigParam.ROOT_PATH_TAG, StrUtil.EMPTY);
            customFiles.forEach(customFile -> objectMap.put(customFile.getFileName().replace(".java",StrUtil.EMPTY), root.concat(StrUtil.DOT).concat(customFile.getPackageName())));
        }
        super.outputCustomFile(customFiles, tableInfo, objectMap);
    }

    @Override
    protected void outputEntity(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap){
        super.outputEntity(tableInfo, objectMap);
    }
}
